package com.vector.controller;

import com.vector.mapper.BookMapper;
import com.vector.pojo.Book;
import com.vector.service.BookService;
import com.vector.service.BookServiceImpl;
import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

@RestController
public class BookController {

    @Autowired
    private BookServiceImpl bookService;

    /**
     * 查询所有图书
     *
     * @return
     */
    @PostMapping("/queryAllBook")
    @ResponseBody
    public List<Book> queryAllBook() {
        List<Book> bookList = bookService.queryAllBook();
//        for ( Book book: bookList) {
//            System.out.println(book);
//        }
        return bookList;
    }

    @GetMapping("/addBook")
    @ResponseBody
    public String addBook(@RequestParam("bookISBN") String bookISBN,
                          @RequestParam("booktype") String booktype,
                          @RequestParam("bookname") String bookname,
                          @RequestParam("bookauthor") String bookauthor,
                          @RequestParam("bookpress") String bookpress,
                          @RequestParam("bookdate") Date bookdate,
                          @RequestParam("bookprintnumber") int bookprintnumber,
                          @RequestParam("bookprice") double bookprice) {
        int i = bookService.addBook(new Book(bookISBN, booktype, bookname, bookauthor, bookpress, bookdate, bookprintnumber, bookprice));
        if (i > 0) {
            return "sucess";
        } else {
            return "error";
        }
    }

    @GetMapping("/updateBook")
    @ResponseBody
    public String updateBook(@RequestParam("bookISBN") String bookISBN,
                          @RequestParam("booktype") String booktype,
                          @RequestParam("bookname") String bookname,
                          @RequestParam("bookauthor") String bookauthor,
                          @RequestParam("bookpress") String bookpress,
                          @RequestParam("bookdate") Date bookdate,
                          @RequestParam("bookprintnumber") int bookprintnumber,
                          @RequestParam("bookprice") double bookprice) {
        int i = bookService.updateBook(new Book(bookISBN, booktype, bookname, bookauthor, bookpress, bookdate, bookprintnumber, bookprice));
        if (i > 0) {
            return "sucess";
        } else {
            return "error";
        }
    }

    @GetMapping("/queryBookById")
    @ResponseBody
    public Book queryBookById(@RequestParam("bookISBN") String bookISBN) {
        Book book = bookService.queryBookById(bookISBN);
        return book;
    }

    @GetMapping("/deleteBook")
    @ResponseBody
    public String deleteBook(@RequestParam("bookISBN") String bookISBN) {
        int i = bookService.deleteBook(bookISBN);
        if (i > 0) {
            return "sucess";
        } else {
            return "error";
        }
    }

    @GetMapping("/queryBookByType")
    @ResponseBody
    public List<Book> queryBookByType(@RequestParam("booktype") String booktype) {
        List<Book> books = bookService.queryBookByType(booktype);
        if (books != null){
            System.out.println(books);
            return books;
        }else {
            return new ArrayList<>();
        }
    }


}
